package com.hhit.collaborative_learning.community;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.hhit.collaborative_learning.dao.BBS_ClassDao;
import com.hhit.collaborative_learning.dao.BBS_TopicDao;
import com.hhit.collaborative_learning.dao.BBS_TopicLableDao;
import com.hhit.collaborative_learning.dao.BBS_TopicStateTyleDao;
import com.hhit.collaborative_learning.dao.UserDao;
import com.hhit.collaborative_learning.dao.impl.BBS_ClassDaoImpl;
import com.hhit.collaborative_learning.dao.impl.BBS_TopicDaoImpl;
import com.hhit.collaborative_learning.dao.impl.BBS_TopicLabelDaoImpl;
import com.hhit.collaborative_learning.dao.impl.BBS_TopicStateTyleDaoImpl;
import com.hhit.collaborative_learning.dao.impl.UserDaoImpl;
import com.hhit.collaborative_learning.utils.DBAccess;
import com.hhit.collaborative_learning.utils.JsonUtil;
import com.hhit.collaborative_learning.utils.ResponseUtil;

/**
 * Servlet implementation class GetForumZTList
 */
@WebServlet("/community/GetForumZTListServlet")
public class GetForumZTListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GetForumZTListServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String class_id = request.getParameter("Class_id");
		String pageNow = request.getParameter("pageNow");
		String pageCount = request.getParameter("pageNum");
		String state = request.getParameter("state");
		String label = request.getParameter("label");
		if ("-1".equals(state)) {
			state = "";
		}else{
			state="like '%"+state+"%' ";
		}
		if ("-1".equals(label)) {
			label = "";
		}
		// 当前页
		int pagenow = Integer.valueOf(pageNow);
		// 页容量
		int pagecount = Integer.valueOf(pageCount);
		BBS_ClassDao bcdao = new BBS_ClassDaoImpl();
		BBS_TopicDao tdao = new BBS_TopicDaoImpl();
		UserDao ud = new UserDaoImpl();
		BBS_TopicLableDao tld = new BBS_TopicLabelDaoImpl();
		BBS_TopicStateTyleDao tstd = new BBS_TopicStateTyleDaoImpl();
		Connection con = DBAccess.getInstance().getConn();
		// 论坛基本数据
		ResultSet rsBBSC = bcdao.queryBBS_Class(con, 0, 0, "ID", "= "
				+ class_id, null, null);
		// 主题总数
		int topic_count = tdao.getcount(con, "ClassID = " + class_id);
		// 按条件调出主题
		ResultSet rsTopic = tdao.queryOther(con, (pagenow - 1) * pagecount,
				(pagenow * pagecount - 1), class_id, label,state,
				"desc", "t.CreatedOn");
		ResultSet rsHotTopic = tdao.queryOther(con, 0, 0, class_id, null,
				" like '%置顶%'", null, null);
		// 查询版主
		ResultSet rsRightUser = ud.queryBBSRightUser(con, 0, 0, "ClassID", "= "
				+ class_id, null, null);
		// 查询该分类的标签
		ResultSet rsTopicLabel = tld.query(con, 0, 0, "ClassID", "= "
				+ class_id, null, null);
		// 查询所有状态
		ResultSet rsTopicStateTyle = tstd.query(con, 0, 0, null, null, null,
				null);
		int pagenum = (int) Math.ceil((double)topic_count / ((double)(Integer.valueOf(pageCount))));
		JSONObject jobj = new JSONObject();
		try {
			JSONArray topicArray = JsonUtil.formatRsToJsonArray(rsTopic);
			JSONArray hotTopicArray = JsonUtil.formatRsToJsonArray(rsHotTopic);
			JSONArray rightUserArray = JsonUtil
					.formatRsToJsonArray(rsRightUser);
			JSONArray topicLabelArray = JsonUtil
					.formatRsToJsonArray(rsTopicLabel);
			JSONArray topicStateTyleArray = JsonUtil
					.formatRsToJsonArray(rsTopicStateTyle);
			jobj = JsonUtil.formatRsToJSONObject(rsBBSC);
			jobj.put("pagenum", pagenum);
			jobj.put("pagenow", pageNow);
			jobj.put("topic", topicArray.toArray());
			jobj.put("hottopic", hotTopicArray.toArray());
			jobj.put("rightuser", rightUserArray.toString());
			jobj.put("topiclabel", topicLabelArray.toString());
			jobj.put("topicstate", topicStateTyleArray.toString());
			ResponseUtil.write(response, jobj);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("respons object :" + jobj.toString());
		} finally {
			DBAccess.getInstance().close(con, null, null);
			System.out.println("respons object :" + jobj.toString());
		}
	}

}
